package com.shengyin.ec.model.dao.goods;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.shengyin.ec.model.dao.BaseDaoImpl;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.News;

@Repository(value = "goodsDao")
public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements GoodsDao {
	

	@Override
	public List<Goods> getAllGoodsList() {
		List<Goods> list = super.loadAll();
		return list;
	}

	@Override
	public List<Goods> getGoodsList(int offset, int pagesize) {
		return super.findByPage("From Goods good",offset, pagesize);
	}

	@Override
	public List<Goods> getGoodsListByKeyword(String keyword, int start, int end) {
		return null;
	}

	@Override
	public List<Goods> getGoodsListByKeyword(String prop, String keyword,
			int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Goods> getGoodsListByType(String typename, int start, int end) {
		return super.findByPage("from Goods g where g.productType='"+typename+"'", start, end);
	}

	@Override
	public void deleteSelectedGoods(Long[] ids) {
		Goods goods=null;
		List<Goods> goodsList=new ArrayList<Goods>(0);
		for(long id:ids)
		{
			goods=super.get(id);
			goodsList.add(goods);
		}
		super.deleteAll(goodsList);
		
	}
	@Override
	public Long getTotal(){
		return (Long) super.find("select count(*) from Goods").get(0);
	}

	
	
	
	//add by lcl 2012-3-17
	@Override
	public Goods getGoodsByProductId(long productId){
		System.out.println("==========================================:"+productId);
		List<Goods> list = super.find("from Goods g where g.id='"+productId+"'");
		return list.get(0);
	}




	@Override
	public List<Goods> getAllGoodsList(int offset, int pagesize) {
		return super.findByPage("from Goods goods", offset, pagesize);
	}
    //add by wql 20120319
	public List<Goods> getGoodsListByType(String typeName){
		return super.find("From Goods g where g.productType='"+typeName+"'");
	}
	
   //add by wql 2012-3-25
	public List<Goods> checkGoodNameExist(String productName){
		
	   
	    
	    return  super.find("from Goods g where g.productName='"+productName+"'");
	}

	@Override
	public List<Goods> getGoodsListByCompanyId(long id) {
		return super.findByPage("from Goods goods where goods.companyId="+id, 0, 1000);
	}

	@Override
	public List<Goods> getGoodsList(long companyid, int offset, int pagesize) {
		return super.findByPage("from Goods goods where goods.companyId="+companyid, 0, 1000);
	}

	@Override
	public void createGoods(Goods goods, long companyid) {
		
	}

	@Override
	public List<Goods> getGoodsListByChainCompanyId(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Goods> getGoodsListBySelected(Long[] idsl) {
		Goods goods=null;
		List<Goods> goodsList=new ArrayList<Goods>(0);
		
		for(Long id:idsl){
			goods=super.get(id);
			goodsList.add(goods);
		}
		
		return goodsList;
	}

    public List<Goods> getGoodsListByCondition(int type,String keyword,int offset,int pageSize){
    
    	switch(type){
    	
    	case 1:
    		return super.findByPage("from Goods where productName like '"+keyword+"'", offset, pageSize);
    	case 2:
    		return super.findByPage("from Goods where productType like '"+keyword+"'", offset, pageSize);
    	case 3:
    		return super.findByPage("from Goods where productCompany like '"+keyword+"'", offset, pageSize);
    	}
    	return null;
    }
	
	public List<Goods> getGoodsListByCondition(int type,String keyword){
    switch(type){
	case 1:
		return super.find("from Goods where productName like '"+keyword+"'");
	case 2:
		return super.find("from Goods where productType like '"+keyword+"'");
	case 3:
		return super.find("from Goods where productCompany like '"+keyword+"'");
	}
	return null;
		
	}
	
	@Override
	public void changeImage(Long id, String image) {
		Goods goods = this.getGoodsByProductId(id);
		goods.setProductPicture(image);
	}

}
